package gui;


import java.awt.Color;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.geom.AffineTransform;

import javax.swing.*;

import javax.swing.JComponent;
import javax.swing.JPanel;

import domain.ConcretePerk;


public class PlayPanel extends JComponent implements MouseMotionListener, KeyListener{

	private Point mouse;
	private Point screen_start;
	private int x;
	private int y;
	private Player p;

	private Map m;
	private WalkHandler wh;
	
	public PlayPanel(){
		mouse = new Point(0,0);
		screen_start = new Point(0,0);
		
		p = new Player(1,100,100,3);

		m = new Map();
		wh = new WalkHandler(p,this,m);
		
		setLayout(null);
		addMouseMotionListener(this);
		
	}
	
	@Override
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		Graphics2D g2d = (Graphics2D) g;
		
		AffineTransform a = AffineTransform.getTranslateInstance(p.getX()-16, p.getY()-16);
		a.concatenate(AffineTransform.getRotateInstance(p.getTeta(mouse.x, mouse.y), 16, 16));
		g2d.drawImage(p.getIcon().getImage(),a,this);
		
		//g2d.fillPolygon(p.createPolygon(mouse.x, mouse.y));
	}

	@Override
	public void mouseDragged(MouseEvent arg0) {
		mouseMoved(arg0);
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		mouse.x = e.getX();
		mouse.y = e.getY();
		//System.out.println(mouse.x + " "+mouse.y);
		//revalidate();
		repaint();
	}

	@Override
	public void keyPressed(KeyEvent e) {
		wh.walk(e.getKeyChar());
		//mouse.x += 20; 
		repaint();
		//System.out.println("baa");
	}

	@Override
	public void keyReleased(KeyEvent e) {
		wh.stopWalking();
	}

	@Override
	public void keyTyped(KeyEvent arg0) {}
	

	public Player getPlayer() {
		return p;
	}
	
	public int getMX(){
		return mouse.x;
	}
	
	public int getMY(){
		return mouse.y;
	}

	public Map getMap() {
		return m;
	}
	
}
